/*    This file is part of jTopaz.
      http://gcl.cis.udel.edu/projects/jtopaz
      Copyright (C) 2009 University of Delaware

      jTopaz is free software: you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      jTopaz is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of                                                        
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with jTopaz.  If not, see <http://www.gnu.org/licenses/>.
*/
package jTopaz;

import org.globus.ftp.GridFTPClient;
import org.globus.ftp.FTPClient;
import org.ietf.jgss.GSSCredential;
import org.globus.myproxy.MyProxy;

/**
   Handles logging onto a gridFTP server.
*/
public class LoginThread extends Thread {
	
    private GridFTPClient client;
    private FTPClient AnonClient;
    private GSSCredential cred;
    private String host;
    private int port;
    private String status;
	
    /**
       @param host the host to connect to.
       @param cred the cred for authentication
    */
    public LoginThread(String host, int port, GSSCredential cred) {
	
	try {
	    this.host = host;
	    System.out.println("host is " + this.host);
	    this.port = port;
	    System.out.println("port is " + this.port);
	    this.cred = cred;
	}
	catch (Exception e) {
	    System.out.println(e);
	}
    }	
	
    /**
       Creates a new GridFTPClient with the given host and port using the cred if it is not null.
    */
    public void run() {
	try {
			
	    System.out.println("creating the client?");
	    System.out.println(host);
	    if(cred!=null){
		System.out.println("cred is NOTNOTNOT null");
		client = new GridFTPClient(host, port);
		System.out.println(cred.getRemainingLifetime());
		client.authenticate(cred);
		client.setPassive();
		client.setLocalActive();
	    }
	    else {
		System.out.println("cred is null");
		AnonClient = new FTPClient(host,port);
		AnonClient.authorize("anonymous","blah");
		AnonClient.setPassive();
		AnonClient.setLocalActive();
		System.out.println("client blah blah blah");
	    }	
	    status = "success";	
	}
	catch (Exception e) {
	    status = "failed";
	    System.out.println("Exception creating client?\n");
	    System.out.println(e);
	}
    }
	
    /**
       Get the status of the login.
       @return "success" if able to log in. "failed" if there was a problem.
    */
    public String getStatus() {
	return status;
    }
	
    /**
       @return the client created during the execution of the thread.
    */
    public GridFTPClient getClient() {
	    return client;
    }
    public FTPClient getAnonClient(){
	return AnonClient;
    }

}